package com.tulele.vtool.task.async;

import com.alibaba.fastjson.JSONObject;
import com.tulele.vtool.bus.model.UserBrandInfo;
import com.tulele.vtool.bus.model.UserInfo;
import com.tulele.vtool.bus.model.thirdparty.WxPushUserInfo;
import com.tulele.vtool.bus.service.ILookArticleService;
import com.tulele.vtool.bus.service.IUserBrandService;
import com.tulele.vtool.bus.service.SysGzhService;
import com.tulele.vtool.model.PlatformSysInfo;
import com.tulele.vtool.util.RedisCacheUtil;
import com.tulele.vtool.util.SysConstant;
import com.tulele.vtool.util.ToolUtil;
import com.tulele.vtool.wechat.service.WeixinService;
import com.tulele.vtool.wechat.service.WeixinServiceDL;
import com.tulele.vtool.wechat.service.WeixinServiceMap;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

@Component
@Slf4j
public class FirstTimeRemindUserActivePushAsyncTask {

    @Autowired
    private RedisCacheUtil redisCacheUtil;

    @Autowired
    private ObjectFactory<WeixinServiceDL> objectFactory;

    @Autowired
    private SysGzhService sysGzhService;

    @Autowired
    private ILookArticleService lookArticleService;

    @Autowired
    private IUserBrandService userBrandService;

    @Value("${wxCode}")
    private String wxCode;

    //20200512 调整参数，多线程跑
    @Async("taskExecutor")
    public void execute(String redisKey, String profile, UserInfo userInfo) throws Exception {

//        log.info("[enter FirstTimeRemindUserActivePushAsyncTask] = " + redisKey);

        Object pushUserJsonObject = redisCacheUtil.getCacheObject(redisKey);
        if (pushUserJsonObject != null) {
            PlatformSysInfo sysGzh = sysGzhService.getPlatformSysInfo(profile);
            String pushUserJson = (String)pushUserJsonObject;
            log.info("[pushUserJson] = " + pushUserJson);

            WxPushUserInfo wxPushUserInfo = JSONObject.parseObject(pushUserJson, WxPushUserInfo.class);

            int lookMeUserCount = Optional.ofNullable(lookArticleService.getLookArticleUserCount(wxPushUserInfo.getUserId())).orElse(0);

            if (lookMeUserCount < 2) {
                List<UserBrandInfo> userBrandInfoList = userBrandService.getUserSubBrandInfo(wxPushUserInfo.getUserId());
                String brandName = "您的";
                if (userBrandInfoList != null && userBrandInfoList.size() > 0) {
                    brandName = userBrandInfoList.get(0).getBrandName();
                }

                String kefuMsg = brandName + SysConstant.FIRST_TIME_REMIND_USER_ACTIVE_MSG.replace(SysConstant.wxCode,wxCode);
                if(userInfo.getPhoneModel().equals(SysConstant.IOS)){
                    kefuMsg = ToolUtil.handleMsgmenuidByIOS(kefuMsg);
                }
                WxMpKefuMessage message = WxMpKefuMessage.TEXT().toUser(wxPushUserInfo.getOpenId()).content(kefuMsg).build();
                try {
                    WeixinServiceDL weixinService= WeixinServiceMap.getWeixinService(profile);
                    if(weixinService==null) {
                        weixinService = objectFactory.getObject();
                        weixinService.reSetConfigStorage(profile);
                        WeixinServiceMap.putWeixinService(profile,weixinService);
                    }

                    weixinService.getKefuService().sendKefuMessage(message);
                } catch (WxErrorException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
